package com.eascs.ticket.project.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.ticket.common.controller.BaseMasterViewController;
import com.eascs.ticket.project.form.SaveProjectForm;
import com.eascs.ticket.project.form.UpdateProjectForm;
import com.eascs.ticket.project.form.ViewProjectForm;
import com.eascs.ticket.project.service.ProjectModuleService;
import com.eascs.ticket.project.service.ProjectService;
import com.eascs.ticket.project.service.ProjectTicketTypeService;
import com.eascs.ticket.project.service.ProjectVersionService;

@Controller
public class ProjectViewController extends BaseMasterViewController {
	@Inject
	private ProjectService projectService;

	@Inject
	private ProjectTicketTypeService projectTicketTypeService;

	@Inject
	private ProjectModuleService projectModuleService;

	@Inject
	private ProjectVersionService projectVersionService;

	@RequestMapping(path = "/project/create")
	public String createProject() {
		ViewContext.put("editType", SaveProjectForm.EDIT_TYPE_CREATE);
		return "project/edit";
	}

	@RequestMapping(path = "/project/update")
	public String updateProject(UpdateProjectForm updateProjectForm) {
		String projectId = updateProjectForm.getProjectId();
		buildProjectViewData(projectId, SaveProjectForm.EDIT_TYPE_UPDATE);
		return "project/edit";
	}

	@RequestMapping(path = "/project/view")
	public String viewProject(ViewProjectForm viewProjectForm) {
		buildProjectViewData(viewProjectForm.getProjectId(), SaveProjectForm.EDIT_TYPE_VIEW);
		return "project/view";
	}

	private void buildProjectViewData(String projectId, String type) {
		ViewContext.put("editType", type);
		ViewContext.put("project", this.projectService.getProjectById(projectId));
		PaginationInfo projectTicketTypePaginationInfo = new PaginationInfo(projectTicketTypeService.getTicketTypeByProjectId(projectId), new String[] { "工单类型" });
		if (SaveProjectForm.EDIT_TYPE_UPDATE.equals(type)) {
			projectTicketTypePaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/project/ticket-type/remove?id=") });
		}
		projectTicketTypePaginationInfo.setSelectDataOptions(new SelectDataOption[] { new SelectDataOption("ticketType") });
		ViewContext.put("projectTicketTypePaginationInfo", projectTicketTypePaginationInfo);

		PaginationInfo projectModulePaginationInfo = new PaginationInfo(projectModuleService.getProjectModuleByProjectId(projectId), new String[] { "名称", "编码", "状态", "备注" });
		if (SaveProjectForm.EDIT_TYPE_UPDATE.equals(type)) {
			projectModulePaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/project/ticket-module/remove?id="), ButtonHelper.createAjaxButton("编辑", "/project/ticket-module/getUpdateProjectModulePage?id=") });
		}
		projectModulePaginationInfo.setSelectDataOptions(new SelectDataOption[] { null, null, new SelectDataOption("status") });
		ViewContext.put("projectModulePaginationInfo", projectModulePaginationInfo);

		PaginationInfo projectVersionPaginationInfo = new PaginationInfo(projectVersionService.getProjectVersionTableViewByProjectId(projectId), new String[] { "版本名称", "计划发布日期", "实际发布日期", "发布人", "生产发布验证日期", "生产发布验证人", "状态", "备注" });
		if (SaveProjectForm.EDIT_TYPE_UPDATE.equals(type)) {
			projectVersionPaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/project/ticket-version/delete?id="), ButtonHelper.createAjaxButton("编辑", "/project/ticket-version/getUpdateProjectVersionPage?id=") });
		}
		projectVersionPaginationInfo.setSelectDataOptions(new SelectDataOption[] { null, null, null, null, null, null, new SelectDataOption("status") });
		ViewContext.put("projectVersionPaginationInfo", projectVersionPaginationInfo);
	}
}
